package com.itheima.service.db.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.domain.db.AnalysisByDay;
import com.itheima.mapper.AnalysisByDayMapper;
import com.itheima.mapper.LogMapper;
import com.itheima.service.db.AnalysisByDayService;
import com.itheima.util.ComputeUtil;
import com.itheima.vo.AnalysisSummaryVo;
import com.itheima.vo.AnnouncementVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author zsk
 * @create 2022-03-26-17:13
 */
@DubboService
public class AnalysisByDayServiceImpl implements AnalysisByDayService {


    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AnalysisByDayMapper analysisByDayMapper;

    //日志同步分析
    @Override
    public void logToAnalysisByDay() {

        //准备基础数据
        //今天数据
        String today = DateUtil.formatDate(new Date());

        //准备昨天数据
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();

        //查询今日log数据
        //查询今日注册数据
        Integer regCount = logMapper.countRegAndLog(today, "0102");
        //查询今日登录数据
        Integer logCount = logMapper.countRegAndLog(today, "0101");
        //查询今天活跃数据
        Integer activeCount = logMapper.countActive(today);
        //查询次日留存
        Integer retention1dCount = logMapper.countRetention1d(today, yesterday);

        //保存并封装
        LambdaQueryWrapper<AnalysisByDay> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnalysisByDay::getRecordDate,today);

        //一天只会添加一次
        AnalysisByDay analysisByDay = analysisByDayMapper.selectOne(wrapper);
        if (analysisByDay == null){//新增
            analysisByDay =  new AnalysisByDay();
            analysisByDay.setRecordDate(new Date()); // 统计日期
            analysisByDay.setNumRegistered(regCount); // 今日注册
            analysisByDay.setNumActive(activeCount);// 今日活跃
            analysisByDay.setNumLogin(logCount); // 今日登录
            analysisByDay.setNumRetention1d(retention1dCount);// 次日留存
            analysisByDayMapper.insert(analysisByDay);// insert语句
        }else {//代表今日以及存在，修改
            analysisByDay.setRecordDate(new Date()); // 统计日期
            analysisByDay.setNumRegistered(regCount); // 今日注册
            analysisByDay.setNumActive(activeCount);// 今日活跃
            analysisByDay.setNumLogin(logCount); // 今日登录
            analysisByDay.setNumRetention1d(retention1dCount);// 次日留存
            analysisByDayMapper.updateById(analysisByDay); // update语句
        }

    }

    // 查询统计数据
    @Override
    public AnalysisSummaryVo findAnalysisSummaryVo() {
        //准备基础数据
        //今天和昨天日期
        String today = DateUtil.offsetDay(new Date(), 0).toDateStr();
        String yesterday = DateUtil.offsetDay(new Date(), -1).toDateStr();

        //前七天
        String weekday = DateUtil.offsetDay(new Date(), -7).toDateStr();
        //前30天
        String monthday = DateUtil.offsetDay(new Date(), -30).toDateStr();

        //今日分析
        AnalysisByDay todayAnalysis = analysisByDayMapper.findAnalysisByDay(today);
        
        //昨天分析
        AnalysisByDay yesterdayAnalysis = analysisByDayMapper.findAnalysisByDay(yesterday);

        //查询九份数据
        Long cumulativeUsers = analysisByDayMapper.sumCumulativeUsers();

        //过去30天活跃用户
        Long activePassMonth = analysisByDayMapper.sumActivePass(monthday, today);

        //过去7天活跃用户
        Long activePassWeek = analysisByDayMapper.sumActivePass(weekday, today);

        //今日新增用户
        Integer newUsersToday = todayAnalysis.getNumRegistered();

        //今日新增用户涨跌率
        BigDecimal newUsersTodayRate = ComputeUtil.computeRate(newUsersToday, yesterdayAnalysis.getNumRegistered());

        //今日登录次数
        Integer loginTimesToday = todayAnalysis.getNumLogin();

        //今日登录涨跌率
        BigDecimal loginTimesTodayRate = ComputeUtil.computeRate(loginTimesToday, yesterdayAnalysis.getNumLogin());

        //今日活跃用户
        Integer activeUsersToday = todayAnalysis.getNumActive();

        //今日活跃用户涨跌率
        BigDecimal activeUsersTodayRate = ComputeUtil.computeRate(activeUsersToday, yesterdayAnalysis.getNumActive());

        //封装并返回Vo对象
        AnalysisSummaryVo vo = new AnalysisSummaryVo();
        vo.setCumulativeUsers(cumulativeUsers);//累计用户
        vo.setActivePassMonth(activePassMonth);//过去30天活跃用户
        vo.setActivePassWeek(activePassWeek);//过去7天活跃用户
        vo.setNewUsersToday(newUsersToday.longValue());//今日新增用户
        vo.setNewUsersTodayRate(newUsersTodayRate);//新增涨跌率
        vo.setLoginTimesToday(loginTimesToday.longValue());//今日登录次数
        vo.setLoginTimesTodayRate(loginTimesTodayRate);//今日登录涨跌率
        vo.setActiveUsersToday(activeUsersToday.longValue());//今日活跃用户
        vo.setActiveUsersTodayRate(activeUsersTodayRate);//活跃用户涨跌率

        return vo;
    }
}
